/* To implement an order book, we maintain two priority queues: one containing
 * bids from higher price to lower price, and the other containing offers from
 * lower price to higher price. Order matching is trivial. To make quotation 
 * fast, we use an array to store the size at each possible price. When we
 * cancel an order, we update its size to zero and leave the rest unchanged.
 *
 * The time complexity of the required operations is as follows:
 * - Best quote: amortized O(1)
 * - Insert: O(log(n)), where p is the number of outstanding orders
 * - Cancel: O(1), where p is the number of outstanding orders
 * - Match: O(log(n)) where n is the number of orders matched
 */

#include <stdio.h>
#include <memory.h>
#include <utility>
#include <queue>
#include <algorithm>

#define MAX_ORDERS  10000
#define MAX_PRICE   99999

struct order
{
    char type;  /* 'B'=buy, 'S'=sell, 'T'=trade, 0=nil */
    int size;   /* 0 <= size <= 99999 */
    int price;  /* 0 <= price <= 99999 */
    order() : type(0), size(0), price(0) { }
};

static order orders[MAX_ORDERS];    /* all orders (0-based) */
static int size_at[MAX_PRICE+1];    /* total size at each price */
static std::priority_queue<std::pair<int,int> > bids;   /* (price, -id) */
static std::priority_queue<std::pair<int,int> > offers; /* (-price, -id) */

static void cancel_order(int id) /* 0-based id */
{
    if (orders[id].size > 0)    /* outstanding */
    {
        size_at[orders[id].price] -= orders[id].size;
        orders[id].size = 0;
    }
}

static void get_quotes(order &bid, order &offer)
{
    /* Remove empty bids and offers. */
    while (!bids.empty() && size_at[bids.top().first] == 0)
        bids.pop();
    while (!offers.empty() && size_at[-offers.top().first] == 0)
        offers.pop();

    bid.price = bids.empty()? 0 : bids.top().first;
    bid.size = bids.empty()? 0 : size_at[bid.price];
    offer.price = offers.empty()? MAX_PRICE : -offers.top().first;
    offer.size = offers.empty()? 0 : size_at[offer.price];
}

static void place_bid(int id, int size, int price) /* 0-based id */
{
    while (size > 0 && !offers.empty() && -offers.top().first <= price)
    {
        int k = -offers.top().second;
        int fill = std::min(orders[k].size, size);
        if (fill > 0)
        {
            printf("TRADE %d %d\n", fill, orders[k].price);
            size_at[orders[k].price] -= fill;
        }
        if ((orders[k].size -= fill) == 0)
            offers.pop();
        size -= fill;
    }
    if (size > 0)
    {
        orders[id].type = 'B';
        orders[id].price = price;
        orders[id].size = size;
        size_at[price] += size;
        bids.push(std::make_pair(price, -id));
    }
}

static void place_offer(int id, int size, int price) /* 0-based id */
{
    while (size > 0 && !bids.empty() && bids.top().first >= price)
    {
        int k = -bids.top().second;
        int fill = std::min(orders[k].size, size);
        if (fill > 0)
        {
            printf("TRADE %d %d\n", fill, orders[k].price);
            size_at[orders[k].price] -= fill;
        }
        if ((orders[k].size -= fill) == 0)
            bids.pop();
        size -= fill;
    }
    if (size > 0)
    {
        orders[id].type = 'S';
        orders[id].price = price;
        orders[id].size = size;
        size_at[price] += size;
        offers.push(std::make_pair(-price, -id));
    }
}

int main()
{
    int n, id;
    scanf("%d", &n);
    memset(size_at, 0, sizeof(size_at));
    for (id = 0; id < n; id++)
    {
        char action[10];
        int size, price;
        scanf("%s %d %d", action, &size, &price);
        if (action[0] == 'C')       /* cancel */
            cancel_order(size - 1); /* size is 1-based order id */
        else if (action[0] == 'B')  /* buy */
            place_bid(id, size, price);
        else if (action[0] == 'S')  /* sell */
            place_offer(id, size, price);

        /* Display quotes. */
        order bid, offer;
        get_quotes(bid, offer);
        printf("QUOTE %d %d - %d %d\n", 
               bid.size, bid.price, 
               offer.size, offer.price);
    }
    return 0;
}